home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / include / FileLogger.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-17  |  6.2 KB  |  190 lines

  1.  
  2. #ifndef __FILELOGGER_H_
  3. #define __FILELOGGER_H_
  4. /*
  5. Peon - Win32 Games Programming Library
  6. Copyright (C) 2002-2005 Erik Yuzwa
  7.  
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License as published by the Free Software Foundation; either
  11. version 2 of the License, or (at your option) any later version.
  12.  
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with this library; if not, write to the Free
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. Erik Yuzwa
  23. peon AT wazooinc DOT com
  24. */
  25.  
  26. #include "peonstdafx.h"
  27.  
  28. #include "ISingleton.h"
  29.  
  30.  
  31. /**
  32. * The following are logging levels supported by the FileLogger.
  33. * The attempt is to use a mask approach to file logging. Let's say
  34. * you release the game using Peon to a customer. Since it's the 
  35. * "production" (or "retail") version of your game, you should only really
  36. * capture critical errors (in order to not affect performance). Then you
  37. * can always get your customer to submit the log file should they experience
  38. * repeated problems.
  39. */
  40. #define PEON_LOG_INFO          0x0001
  41. #define PEON_LOG_DEBUG           0x0002
  42. #define PEON_LOG_ERROR         0x0004
  43. #define PEON_LOG_FATAL         0x0008
  44.  
  45.  
  46. namespace peon {
  47.  
  48.     /**
  49.     *
  50.     * This object is just used to output any needed log messages to 
  51.     * a file. We COULD make this fancier (like a primitive Log4J 
  52.     * solution) by perhaps creating a LogInterface superclass, which
  53.     * we can then derive FileLogger from. This would then allow us
  54.     * to also create an SMTPLogger or HTMLLogger for example (to output
  55.     * our log messages over SMTP or into a nice and purty HTML document).
  56.     *
  57.     * I made derived it from the ISingleton object in order to be 
  58.     * accessible pretty much everywhere in our game and/or Peon.
  59.     */
  60.     class PEONMAIN_API FileLogger : public ISingleton<FileLogger>
  61.     {
  62.  
  63.     protected:
  64.         
  65.         /** the file handle */
  66.         std::ofstream    m_log_file;
  67.  
  68.         /** name of the file */
  69.         String            m_strLogName;
  70.     
  71.         /** our logging level or filter */
  72.         int                m_logging_level;
  73.     
  74.     protected:
  75.  
  76.         /**
  77.         * This method is responsible for writing out text to our
  78.         * logfile
  79.         * @param String - our desired text to append
  80.         * @return void
  81.         */
  82.         void writeToLogStream(const String& strText);
  83.  
  84.         
  85.     public:
  86.  
  87.         /**
  88.         * Constructor
  89.         * @param log_flag - the logging filter level
  90.         */
  91.         FileLogger( int log_flag = PEON_LOG_DEBUG );
  92.  
  93.         /**
  94.         * Destructor
  95.         */
  96.         ~FileLogger();
  97.  
  98.         /** Override standard Singleton retrieval.
  99.             @remarks
  100.                 Why do we do this? Well, it's because the Singleton
  101.                 implementation is in a .h file, which means it gets compiled
  102.                 into anybody who includes it. This is needed for the
  103.                 Singleton template to work, but we actually only want it
  104.                 compiled into the implementation of the class based on the
  105.                 Singleton, not all of them. If we don't change this, we get
  106.                 link errors when trying to use the Singleton-based class from
  107.                 an outside dll.
  108.             @par
  109.                 This method just delegates to the template version anyway,
  110.                 but the implementation stays in this single compilation unit,
  111.                 preventing link errors.
  112.         */
  113.         static FileLogger& getSingleton(void);
  114.  
  115.         /** Override standard Singleton retrieval.
  116.             @remarks
  117.                 Why do we do this? Well, it's because the Singleton
  118.                 implementation is in a .h file, which means it gets compiled
  119.                 into anybody who includes it. This is needed for the
  120.                 Singleton template to work, but we actually only want it
  121.                 compiled into the implementation of the class based on the
  122.                 Singleton, not all of them. If we don't change this, we get
  123.                 link errors when trying to use the Singleton-based class from
  124.                 an outside dll.
  125.             @par
  126.                 This method just delegates to the template version anyway,
  127.                 but the implementation stays in this single compilation unit,
  128.                 preventing link errors.
  129.         */
  130.         static FileLogger* getSingletonPtr(void);
  131.  
  132.         /**
  133.         * This method simply opens the file handle and prepares the
  134.         * log file for writing
  135.         * @param std::string - our desired filename
  136.         * @return bool - true if we succeeded, error code otherwise
  137.         */
  138.         bool openLogStream(const String& strName);
  139.  
  140.         /**
  141.         * This method is responsible for shutting down our logging
  142.         * and closing any file handles
  143.         * @param void
  144.         * @return void
  145.         */
  146.         void closeLogStream(void);
  147.  
  148.         /**
  149.         * This method logs a string setting it to "Info" mode. It then
  150.         * compares it with the internal log setting to see if it should
  151.         * be recorded or not.
  152.         * @param strObject - object making logging call
  153.         * @param strText - text to output
  154.         */
  155.         void logInfo ( const String& strObject, const String& strText);
  156.  
  157.         /**
  158.         * This method logs a string setting it to "Debug" mode. It then
  159.         * compares it with the internal log setting to see if it should
  160.         * be recorded or not.
  161.         * @param strObject - object making logging call
  162.         * @param strText - text to output
  163.         */
  164.         void logDebug(const String& strObject, const String& strText);
  165.  
  166.         /**
  167.         * This method logs a string setting it to "Error" mode. It then
  168.         * compares it with the internal log setting to see if it should
  169.         * be recorded or not.
  170.         * @param strObject - object making logging call
  171.         * @param strText - text to output
  172.         */
  173.         void logError(const String& strObject, const String& strText);
  174.  
  175.         /**
  176.         * This method logs a string setting it to "Fatal" mode. It then
  177.         * compares it with the internal log setting to see if it should
  178.         * be recorded or not.
  179.         * @param strObject - object making logging call
  180.         * @param strText - text to output
  181.         */
  182.         void logFatal( const String& strObject, const String& strText);
  183.  
  184.     
  185.         
  186.     };
  187. }
  188.  
  189. #endif
  190.